package com.blog.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.blog.BO.NoteBO;
import com.blog.dao.BaseDao;
import com.blog.dao.NoteDao;
import com.blog.entity.Note;

public class NoteDaoImpl implements NoteDao {

	@Override
	public Note selectLastNote() {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT * FROM notification ORDER BY noteDate DESC LIMIT 0,1;";
		Note lastNote = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				lastNote = new Note();
				lastNote.setNoteId(res.getInt("noteId"));
				lastNote.setNoteText(res.getString("noteText"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("noteDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				lastNote.setNoteDate(dt);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return lastNote;
	}

	@Override
	public List<Note> selectNoteByNoteBO(NoteBO noteBO, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Note> noteList = new ArrayList<Note>();
		List<Object>  params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("SELECT * FROM discuss where 1 = 1");
		if(noteBO != null) {
			//查询条件
			if(noteBO.getNoteId() != 0) {
				sql.append(" AND noteId = ?");
				params.add(noteBO.getNoteId());
			}//通知id
			if(noteBO.getNoteText()!=null && !"".equals(noteBO.getNoteText())) {
				sql.append(" AND noteText LIKE concat('%',?,'%')");
				params.add(noteBO.getNoteText());
			}//通知内容
			if(noteBO.getNoteDate()!=null) {
				sql.append(" AND noteDate = ?");
				params.add(noteBO.getNoteDate());
			}//通知创建时间
			if(noteBO.getNoteStartDate()!=null) {
				sql.append(" AND noteDate >= ?");
				params.add(noteBO.getNoteStartDate());
			}//通知从什么时间开始，包含输入的时间
			if(noteBO.getNoteEndDate()!=null) {
				sql.append(" AND noteDate <= ?");
				params.add(noteBO.getNoteEndDate());
			}//通知从什么时候结束，包含输入的时间
		}
		sql.append(" LIMIT ?,?");
		Note selectNote = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql.toString());
			for(int i =0;i<params.size();i++) {
				ps.setObject(i+1, params.get(i));
			}
			ps.setInt(params.size()+1, (pageNum-1)*pageSize);
			ps.setInt(params.size()+2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				selectNote = new Note();
				selectNote.setNoteId(res.getInt("noteId"));
				selectNote.setNoteText(res.getString("noteText"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("noteDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				selectNote.setNoteDate(dt);
				noteList.add(selectNote);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return noteList;
	}

	@Override
	public int insertNote(Note note) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "INSERT INTO notification(noteText,noteDate) VALUES (?,?);";
		int resultNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,note.getNoteText());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dt = format.format(note.getNoteDate().getTime());
			ps.setString(2,dt);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int deleteNoteByNoteId(NoteBO noteBO) {
		// TODO Auto-generated method stub
		List<Object>  params = new ArrayList<Object>();
		int resultNum = 0;
		Connection con = null;
		PreparedStatement ps = null;
		StringBuffer sql = new StringBuffer("DELETE FROM notification WHERE 1 = 1");
		if(noteBO.getNoteId() != 0) {
			sql.append(" AND noteId = ?");
			params.add(noteBO.getNoteId());
		}//通知id
		if(noteBO.getNoteText()!=null && !"".equals(noteBO.getNoteText())) {
			sql.append(" AND noteText = ?");
			params.add(noteBO.getNoteText());
		}//通知内容
		if(noteBO.getNoteDate()!=null) {
			sql.append(" AND noteDate = ?");
		}//通知创建时间
		if(noteBO.getNoteStartDate()!=null) {
			sql.append(" AND noteDate >= ?");
		}//通知从什么时间开始，包含输入的时间
		if(noteBO.getNoteEndDate()!=null) {
			sql.append(" AND noteDate <= ?");
		}//通知从什么时候结束，包含输入的时间
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql.toString());
			for(int i =0;i<params.size();i++) {
				ps.setObject(i+1, params.get(i));
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dt1 = format.format(noteBO.getNoteDate().getTime());
			ps.setString(params.size()+1,dt1);
			String dt2 = format.format(noteBO.getNoteStartDate().getTime());
			ps.setString(params.size()+2,dt2);
			String dt3 = format.format(noteBO.getNoteEndDate().getTime());
			ps.setString(params.size()+3,dt3);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int updateNote(Note note) {
		// TODO Auto-generated method stub
		List<Object>  params = new ArrayList<Object>();
		int resultNum = 0;
		if(note.getNoteId() != 0) {
			Connection con = null;
			PreparedStatement ps = null;
			StringBuffer sql = new StringBuffer("UPDATE notification SET noteId = ?");
			params.add(note.getNoteId());
			if(note.getNoteText()!=null && !"".equals(note.getNoteText())) {
				sql.append(",noteText = ?");
				params.add(note.getNoteText());
			}//通知内容
			sql.append(",noteDate = ?");
			sql.append(" WHERE noteId = ?");
			try {
				con = BaseDao.getCon();
				ps = con.prepareStatement(sql.toString());
				for(int i =0;i<params.size();i++) {
					ps.setObject(i+1, params.get(i));
				}
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dt = format.format(new Date().getTime());
				ps.setString(params.size()+1,dt);
				ps.setInt(params.size()+2,note.getNoteId());
				resultNum = ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				BaseDao.closeAll(null, ps, con);
			}
		}
		return resultNum;
	}

}
